package cn.tedu;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;

public class JedisTest {
    private Jedis jedis;
    @BeforeEach //每个方法执行之前执行的方法
    public void setUp() {
        jedis = new Jedis("192.168.8.100",6379);
        jedis.auth("123456");
        System.out.println("连接Redis服务器成功");

    }

    @Test
    public void setString(){
        String result = jedis.set("test:name","maxn");
        System.out.println("result="+result);
        String testName = jedis.get("test:name");
        System.out.println("获取到的数据为："+testName);
    }

    @Test
    public void setNX(){
        Long result = jedis.setnx("test:age1","18");
        if(result==0){
            System.out.println("改值已存在");
        }else{
            System.out.println("test:age1的值是"+jedis.get("test:age1"));
            System.out.println("改值不存在，设置成功");
        }
    }

    @AfterEach  //每个方法执行之后执行的方法
    public void setDown() {
        if (jedis != null) {
            jedis.close();
        }
        System.out.println("释放资源");
    }
}
